package user.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import user.dao.BookAdminDao;
import user.dao.BookDao;
import user.dao.UserDao;
import user.db.DBConnection;
import user.model.Book;
import user.model.Borrow;
import user.model.User;
import user.util.StringUtil;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.security.ProtectionDomain;
import java.sql.Connection;
import java.sql.ResultSet;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;

public class BookSelectFrame extends JInternalFrame {

	private JTable table;
	private UserDao userDao = new UserDao();
	private BookDao bookDao = new BookDao();
	private BookAdminDao bookAdminDao = new BookAdminDao();
	private JTextField bookIdTxt;
	private JTextField authorTxt;
	private JTextField lendTxt;
	private JTextField bookNameTxt;
	private static String loginName = null;
	private JTextField bookName;
	private JTextField author;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookSelectFrame frame = new BookSelectFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookSelectFrame() {
		setClosable(true);
		setIconifiable(true);
		setTitle("图书查询");
		setBounds(100, 100, 642, 629);
		
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel label4 = new JLabel("图书名称");
		label4.setFont(new Font("宋体", Font.PLAIN, 18));
		
		bookIdTxt = new JTextField();
		bookIdTxt.setFont(new Font("宋体", Font.PLAIN, 18));
		bookIdTxt.setEditable(false);
		bookIdTxt.setColumns(10);
		
		JLabel label1 = new JLabel("图书作者");
		label1.setFont(new Font("宋体", Font.PLAIN, 18));
		
		authorTxt = new JTextField();
		authorTxt.setFont(new Font("宋体", Font.PLAIN, 18));
		authorTxt.setEnabled(true);
		authorTxt.setEditable(false);
		authorTxt.setText("");
		authorTxt.setColumns(10);
		
		JLabel label2 = new JLabel("是否在馆");
		label2.setFont(new Font("宋体", Font.PLAIN, 18));
		
		lendTxt = new JTextField();
		lendTxt.setFont(new Font("宋体", Font.PLAIN, 18));
		lendTxt.setEditable(false);
		lendTxt.setColumns(10);
		
		JButton button = new JButton("借阅");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row = table.getSelectedRow();
				if(table.isRowSelected(row)==false) {
					JOptionPane.showMessageDialog(null, "请选择一行");
				}
				String lend = lendTxt.getText();
				if(lend.equals("否")) {
					JOptionPane.showMessageDialog(null, "此书已经借出，请重新选择一本书!");
					reset();
				}
				else {
					String x = JOptionPane.showInputDialog(null, "请输入密钥");
					if(x.equals("123456")) {
					BorrowBookActionPerformed(e);
				}
				else {
					JOptionPane.showMessageDialog(null, "密钥错误，借阅失败！");
				}
				}
				
				
				
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 22));
		
		JButton button1 = new JButton("查询");
		button1.setIcon(new ImageIcon(BookSelectFrame.class.getResource("/images/查询.png")));
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SelectBookActionPerformed(e);
			}
		});
		button1.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JButton button2 = new JButton("返回");
		button2.setIcon(new ImageIcon(BookSelectFrame.class.getResource("/images/返回.png")));
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		button2.setFont(new Font("宋体", Font.PLAIN, 21));
		
		JLabel label3 = new JLabel("图书编号");
		label3.setFont(new Font("宋体", Font.PLAIN, 18));
		
		bookNameTxt = new JTextField();
		bookNameTxt.setFont(new Font("宋体", Font.PLAIN, 18));
		bookNameTxt.setEditable(false);
		bookNameTxt.setColumns(10);
		
		JLabel label5 = new JLabel("图书名称");
		label5.setFont(new Font("宋体", Font.PLAIN, 18));
		
		bookName = new JTextField();
		bookName.setFont(new Font("宋体", Font.PLAIN, 18));
		bookName.setColumns(10);
		
		JLabel label6 = new JLabel("作者");
		label6.setFont(new Font("宋体", Font.PLAIN, 18));
		
		author = new JTextField();
		author.setFont(new Font("宋体", Font.PLAIN, 18));
		author.setColumns(10);
		GroupLayout gl_contentPane = new GroupLayout(getContentPane());
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 598, Short.MAX_VALUE)
						.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(label3)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(bookIdTxt))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(label4)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addComponent(button)
										.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE))))
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(66)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
										.addGroup(gl_contentPane.createSequentialGroup()
											.addComponent(label1)
											.addGap(18)
											.addComponent(authorTxt))
										.addGroup(gl_contentPane.createSequentialGroup()
											.addComponent(label2)
											.addGap(18)
											.addComponent(lendTxt, 129, 129, 129))))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(134)
									.addComponent(button2))))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(label5)
							.addGap(17)
							.addComponent(bookName, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
							.addComponent(label6)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(author, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
							.addGap(84)
							.addComponent(button1)))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(35)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label5)
						.addComponent(button1)
						.addComponent(label6)
						.addComponent(author, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 244, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(label3)
						.addComponent(bookIdTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label1)
						.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(label4)
						.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label2)
						.addComponent(lendTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(55)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(button)
						.addComponent(button2))
					.addGap(73))
		);
		
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				BookMousePressed(e);
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u56FE\u4E66\u7F16\u53F7", "\u56FE\u4E66\u540D\u79F0", "\u56FE\u4E66\u7C7B\u578B", "\u56FE\u4E66\u4F5C\u8005", "\u56FE\u4E66\u4EF7\u683C", "\u5728\u9986"
			}
		));
		scrollPane.setViewportView(table);
		getContentPane().setLayout(gl_contentPane);
		fillTable("","");
	}
	
	

	protected void SelectBookActionPerformed(ActionEvent e) {
		String bookName = this.bookName.getText();
		String author = this.author.getText();
		fillTable(bookName, author);
	}

	protected void BorrowBookActionPerformed(ActionEvent e) {
		int bookid = Integer.parseInt(this.bookIdTxt.getText());//获得图书id
		String bookName = this.bookNameTxt.getText();
		String author = this.authorTxt.getText();
		String lend = this.lendTxt.getText();
		//书是否已经借出
		Book book = new Book();
		book = bookDao.SelectBook();
		if(lend.equals("否")) {//书借出了
			JOptionPane.showMessageDialog(null, "此书已经借出，请重新选择一本书!");
			reset();
			
		}else {//书没有被借出 可以借阅
			int i = bookDao.UpdateBookLend(bookid);
			if(i>0) {
				loginName = LoginFrame.userName();
				User u = userDao.selectUname(loginName);
				String id = u.getId();
				int j = bookDao.insertBorrow(id, bookid);
				if(j>0) {
					JOptionPane.showMessageDialog(null, "借书成功！");
					reset();
					fillTable("", "");
				}
			}
		}
	}
	

	protected void BookMousePressed(MouseEvent e) {
		int row = table.getSelectedRow();//获得行号
		bookIdTxt.setText(String.valueOf(table.getValueAt(row, 0)));
		bookNameTxt.setText((String) table.getValueAt(row, 1));//获取第一列信息
		authorTxt.setText((String) table.getValueAt(row, 2));
		lendTxt.setText(String.valueOf(table.getValueAt(row, 5)));
		
	}

	public void fillTable(String bookName,String author) {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
	    dtm.setRowCount(0);//设置成0行
	    //查询出书籍
	    List<Book> bookList = bookAdminDao.listBook(bookName, author);
	    for(Book book:bookList) {
	    	Vector v = new Vector<>();
	    	v.add(dtm.getRowCount()+1);
	    	v.add(book.getBookName());
	    	v.add(book.getAuthor());
	    	v.add(book.getType());
	    	v.add(book.getPrice());
	    	if(Integer.valueOf(book.getLend()).equals(0)) {
	    		v.add("否");
			}
			else {
				v.add("是");
			}
	    	dtm.addRow(v);
	    }
	}
	public void reset() {
		bookIdTxt.setText("");
		bookNameTxt.setText("");
		authorTxt.setText("");
		lendTxt.setText("");
	}
}
